// pages/message/index.js
import {
  list,
  markAsRead,
  unReadCount,
  conversation
} from "../../../api/msgPush"
import util from '../../../utils/util'
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    messageOrder:{},
    logistic:{},
    customer:{},
    admin:{},
    account:{},
    messages:[],
    unReads: [],
    orderCount: '',
    logisticsCount: '',
    customerCount: '',
    accountCount: '',
    adminCount: ''
  },
  backClick() {

  },
  infoClick() {
    console.log(12);
  },
  //每种消息的最后一条
  loadMessage(){
    conversation({
      mchId: app.globalData.mchId,
    }).then(res=>{
      res.data.forEach(item=>{
        item.sendDate=util.datetimeFormat(item.sendDate)
        item.msgContent=JSON.parse(item.msgContent)
        if(item.msgKey=='msg-order'){
          this.data.messageOrder=item
        }else if(item.msgKey=='msg-shipping'){
          this.data.logistic=item
        }else if(item.msgKey=='msg-account'){
          this.data.account=item
        }else if(item.msgKey=='msg-service'){
          this.data.customer=item
        }else if(item.msgKey=='msg-notify'){
          this.data.admin=item
        }
      })
      console.log(res.data)
        this.setData({
          messageOrder:this.data.messageOrder,
          logistic:this.data.logistic,
          account:this.data.account,
          customer:this.data.customer,
          admin:this.data.admin
        })
    })
  },

  //未读消息书
  loadUnReadCount() {
    unReadCount().then(res => {
      this.data.unReads = res.data
      if(res.data.length>0){
        this.data.unReads.forEach(item => {
          switch (item.msg_key) {
            case 'msg-order':
              this.data.orderCount = item.msg_num
              break;
            case 'msg-shipping':
              this.data.logisticsCount = item.msg_num
              break;
            case 'msg-account':
              this.data.accountCount = item.msg_num
              break;
            case 'msg-service':
              this.data.customerCount = item.msg_num
              break;
            case 'msg-notify':
              this.data.adminCount = item.msg_num
              break;
          }
        })
      }else{
        this.data.orderCount='',
        this.data.logisticsCount='',
        this.data.accountCount='',
        this.data.customerCount='',
        this.data.adminCount=''
      }
      this.setData({
        orderCount:this.data.orderCount,
        logisticsCount:this.data.logisticsCount,
        accountCount:this.data.accountCount,
        adminCount:this.data.adminCount,
        customerCount:this.data.customerCount,
        unReads: this.data.unReads,
      })
    })
  },

  toOrder() {
    this.loadUnReadCount()
    wx.navigateTo({
      url: '/pages/member/message/messageOrder/messageOrder'
    })
  },
  toLogistics() {
    this.loadUnReadCount()
    wx.navigateTo({
      url: '/pages/member/message/logistics/logistics'
    })
  },
  toAccount() {
    this.loadUnReadCount()
    wx.navigateTo({
      url: '/pages/member/message/account/account'
    })
  },
  toCustomer() {
    this.loadUnReadCount()
    wx.navigateTo({
      url: '/pages/member/message/customer/customer'
    })
  },
  toAdmin() {
    this.loadUnReadCount()
    wx.navigateTo({
      url: '/pages/member/message/admin/admin'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.loadUnReadCount()
  },
        onShow: function (options) {
                this.loadMessage()
                this.loadUnReadCount()
        },

  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})